package cn.itcast.web.controller.cargo;

import cn.itcast.common.utils.ImageUploadUtils;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExtCProductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {

	@Reference
	private ExtCProductService extCProductService;

	@Reference
	private FactoryService factoryService;

	/**
	 * 分页展示某个货物下的所有附件
	 *      参数 ： 货物ID
	 *          :  合同ID
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size,
                       String contractProductId,String contractId) {
		//查询所有附件
		ExtCproductExample example = new ExtCproductExample();
		ExtCproductExample.Criteria criteria = example.createCriteria();
		criteria.andContractProductIdEqualTo(contractProductId);
		PageInfo info = extCProductService.findAll(page, size, example);
		request.setAttribute("page",info);

		//查询所有生产附件的厂家
		FactoryExample factoryExample = new FactoryExample();
		FactoryExample.Criteria exampleCriteria = factoryExample.createCriteria();
		exampleCriteria.andCtypeEqualTo("附件");
		List factoryList = factoryService.findAll(factoryExample);
		request.setAttribute("factoryList",factoryList);

		//设置合同id和货物id
		request.setAttribute("contractProductId",contractProductId);
		request.setAttribute("contractId",contractId);

		//页面跳转
		return "cargo/extc/extc-list";
	}

	/**
	 * 进入到修改附件的页面
	 *  参数：
	 *      附件id：id
	 *      合同id：contractId
	 *      货物id：contractProductId
	 */
	@RequestMapping("/toUpdate")
	public String toUpdate(String id,String contractId,String contractProductId) {
		//1.根据id查询附件
		ExtCproduct extCproduct = extCProductService.findById(id);
		request.setAttribute("extCproduct",extCproduct);
		//2.查询所有生产附件的厂家
		FactoryExample factoryExample = new FactoryExample();
		FactoryExample.Criteria exampleCriteria = factoryExample.createCriteria();
		exampleCriteria.andCtypeEqualTo("附件");
		List factoryList = factoryService.findAll(factoryExample);
		request.setAttribute("factoryList",factoryList);
		//3.设置id参数
		request.setAttribute("contractId",contractId);
		request.setAttribute("contractProductId",contractProductId);
		return "cargo/extc/extc-update";
	}

	/**
	 * 保存或者更新附件
	 *      参数：附件对象
	 *              包含了货物id：contractProductId
	 *              包含了合同id：contractId
	 */
	@RequestMapping("/edit")
	public String edit(ExtCproduct extCproduct) throws IOException {

		extCproduct.setCompanyId(getLoginCompanyId());
		extCproduct.setCompanyName(getLoginCompanyName());

		if(StringUtils.isEmpty(extCproduct.getId())) {
			//保存
			extCProductService.save(extCproduct);
		}else{
			//更新
			extCProductService.update(extCproduct);
		}
		return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+
				"&contractProductId="+extCproduct.getContractProductId();
	}

	/**
	 * 根据id删除
	 *      参数：
	 *          附件ID：id
	 *          为了方便页面跳转，加入以下两个参数
	 *          合同id：contractId
	 *          货物id：contractProductId
	 */
	@RequestMapping("/delete")
	public String delete(String id,String contractId,String contractProductId) {
		extCProductService.delete(id);
		return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
	}
}
